package com.mkz.storage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.storage.entity.Storage;
import com.mkz.storage.service.StorageService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 库存表
 *
 * @author Kuangjianyu
 * @date 2020-05-06 13:55:11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/storage" )
@Api(value = "storage", tags = "库存表管理")
public class StorageController {

    private final  StorageService storageService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param storage 库存表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult getStoragePage(Page page, Storage storage) {
        return ResponseResult.ok(storageService.page(page, Wrappers.query(storage)));
    }


    /**
     * 通过id查询库存表
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult getById(@PathVariable("id" ) Integer id) {
        return ResponseResult.ok(storageService.getById(id));
    }

    /**
     * 新增库存表
     * @param storage 库存表
     * @return ResponseResult
     */
    @ApiOperation(value = "新增库存表", notes = "新增库存表")
    @SysLog("新增库存表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('storage_storage_add')" )
    public ResponseResult save(@RequestBody Storage storage) {
        return ResponseResult.ok(storageService.save(storage));
    }

    /**
     * 修改库存表
     * @param storage 库存表
     * @return ResponseResult
     */
    @ApiOperation(value = "修改库存表", notes = "修改库存表")
    @SysLog("修改库存表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('storage_storage_edit')" )
    public ResponseResult updateById(@RequestBody Storage storage) {
        return ResponseResult.ok(storageService.updateById(storage));
    }

    /**
     * 通过id删除库存表
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除库存表", notes = "通过id删除库存表")
    @SysLog("通过id删除库存表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('storage_storage_del')" )
    public ResponseResult removeById(@PathVariable Integer id) {
        return ResponseResult.ok(storageService.removeById(id));
    }

}
